var adTable;
/****列表信息*/
function showAds() {
	showLoading("查询中...");
	$.ajax( {
		url : 'advertisement/listPage.do',
		type : 'get',
		dataType : 'html',
		async:true,
		success : function(html) {
			$('#mainForm').html(html);
			queryAdDatas();
			hideLoading();
			//绑定查询按钮 
		$('#searchAdBtn').click(function() {
			queryAdPage();
		});
	}
	});
}
function queryAdPage() {
	showLoading("查询中...");
	queryAdDatas();
	hideLoading();
}
function queryAdDatas() {
	adTable = $('#entTable').table( {
		width : '100%',
		defaultOrderBy : "sort",
		defaultOrderType : "asc",
		form : {
			url : 'advertisement/queryPage.do',
			id : '#entSearchForm',
			type : 'post',
			pageNumberText : '#pageNum'
		},
		pageable : true,
		selectable : true,
		colums : [ {
			columnName : '名称',
			name : 'name',
			handle : "toAddAd"
		}, {
			columnName : '创建时间',
			name : 'crateDate'
		}, {
			columnName : '更新时间',
			name : 'updateDate'
		}, {
			columnName : '显示状态',
			name : 'state',
			formatter : function(obj) {
				if (obj.state == '1') {
					return "显示";
				} else if (obj.state == '0') {
					return "不显示";
				} else {
					return "";
				}
			}
		} ]
	});
}

function toAddAd(id) {
	var param = '?t=' + ts();
	if (id) {
		param += '&id=' + id;
	}
	$.get('advertisement/toAddAd.do' + param, function(html) {
		$('#commonDialogDiv').html(html);
		$("#list2_mul").dragsort({ dragSelector: "div", dragBetween: true, placeHolderTemplate: "<li class='placeHolder'><div></div></li>" });
		$('#commonDialogDiv').dialog( {
			modal : true,
			title : '广告信息',
			width : 995,
			height : 600,
			buttons : {
				'保存' : function() {
					saveAddAd();
				},
				'取消' : function() {
					$(this).dialog('close');
				}
			}
		});
		validator = $("#adForm").validate( {
			rules : {
				"model.name" : {
					required : true,
					maxlength : 20
				},
				"model.detailInfo" : {
					maxlength : 150
				}
			},
			messages : {
				"model.name" : {
					required : "必填"
				},
				"model.detailInfo" : {
					maxlength : "最长不超过150字"
				}
			}
		});
	});
}

function saveAddAd() {
	if (validator.form()) {
		$('#adForm').ajaxSubmit( {
			url : 'advertisement/saveAd.do',
			type : 'post',
			dataType : 'json',
			success : function(data) {
				if (data.success) {
					showDialog('保存成功');
					$('#commonDialogDiv').dialog('close');
					queryAdPage();
				} else {
					showDialog('保存失败，' + data.message);
				}
			}
		});
	}
}

//批量删除 
function toDeleteAd() {
	var ids = '';
	$('.ids').each(function() {
		if (this.checked == true) {
			ids += 'ids=' + this.value + '&';
		}
	});
	if (ids.length == 0) {
		showDialog('请至少选择一条记录！');
		return;
	}
	$('#commonDialogDiv').html('<p>确定要删除记录信息？</p>');
	$('#commonDialogDiv').dialog( {
		modal : true,
		width : 250,
		height : 160,
		buttons : {
			'确定' : function() {
				$(this).dialog('close');
				$.ajax( {
					url : 'advertisement/delete.do?' + ids,
					type : 'get',
					dataType : 'json',
					success : function(data) {
						if (data.success) {
							queryAdPage();
						} else {
							showDialog('删除失败，原因：' + data.message);
						}
					}
				});
			},
			'取消' : function() {
				$(this).dialog('close');
			}
		}
	});
}